home *** CD-ROM | disk | FTP | other *** search
/ IRIX Installation Tools & Overlays 2001 May / SGI IRIX Installation Tools & Overlays 2001 May - Disc 3.iso / relnotes / dmedia_dev / ch08.z / ch08
Text File  |  2001-04-16  |  95KB  |  1,651 lines

  1.  
  2.  
  3.  
  4.                                   - 1 -
  5.  
  6.  
  7.  
  8.        9.  _A_u_d_i_o__F_i_l_e__L_i_b_r_a_r_y
  9.  
  10.        This chapter lists information about the Audio File Library
  11.        programming interface included in the the IRIS Digital Media
  12.        Development Environment.  It includes changes, additions,
  13.        and bug fixes since the last release, and known problems,
  14.        workarounds, and caveats.
  15.  
  16.        The Audio File Library consists of approximately 100 C
  17.        routines that allow application programs to read/write audio
  18.        sample data and auxiliary header information to/from a
  19.        number of standard audio file formats.  The library requires
  20.        no special audio hardware at runtime, and is useful for
  21.        sound file import/export/conversion operations as well as
  22.        real-time recording/playback.
  23.  
  24.        The Audio File Library API allows you to access audio
  25.        samples and nonaudio information from audio files using a
  26.        common set of routines.  The API includes functions for
  27.        reading/writing various types of data including author,
  28.        copyright, name, and annotation strings; sample frame marker
  29.        locations; sample configuration parameters and loop points;
  30.        MIDI exclusive data; and application-defined data.
  31.  
  32.        The Audio File Library implements transparent sample data
  33.        format conversion and compression/decompression for many
  34.        different audio file formats.  The following compression
  35.        algorithms are currently supported:
  36.  
  37.           +o MPEG-1 Layers I and II
  38.  
  39.           +o Aware Inc.'s, proprietary MultiRate algorithm
  40.  
  41.           +o ITU G.711 mu-law and A-law
  42.  
  43.           +o ITU G.722 ADPCM
  44.  
  45.           +o ITU G.726 ADPCM
  46.  
  47.           +o ITU G.728 ADPCM
  48.  
  49.           +o GSM 06.10
  50.  
  51.           +o IMA DVI ADPCM
  52.  
  53.           +o US Federal Standard 1016 CELP
  54.  
  55.        Transparent format conversion and compression/decompression
  56.        means that application programs can process all audio files
  57.        as uncompressed data in any format that the application
  58.        desires.  There is no need to write special code for
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                                   - 2 -
  71.  
  72.  
  73.  
  74.        handling different sample formats or types of compressed
  75.        data.  Compressed and decompressed files look the same to
  76.        applications as long as the compression format is one of the
  77.        supported types listed above.
  78.  
  79.        Note:  The Aware MultiRate codec cannot be activated by an
  80.               application without a FlexLM  node-locked license,
  81.               available from SGI.
  82.  
  83.               See the online man page _a_w_a_r_e(_5) for an overview of
  84.               the Aware compression technology available to
  85.               developers and end users. The online man page
  86.               _a_f_I_n_i_t_C_o_m_p_r_e_s_s_i_o_n(3dm) describes the Audio File
  87.               Library interface to reading/writing audio files that
  88.               contain MultiRate or other types of compressed data.
  89.  
  90.        The Audio File Library API is intended to be general enough
  91.        to allow support for additional digital audio file formats
  92.        and compression schemes in the future.  Applications that
  93.        developed using the current version of the library will be
  94.        able to read these additional file and data formats as new
  95.        versions of the Audio File Library are released, without
  96.        need for re-linking.
  97.  
  98.        Applications linked with the 6.X Audio File Library no
  99.        longer need to be linked with any other library.  All that
  100.        is needed on the link line is:
  101.  
  102.        ----llllaaaauuuuddddiiiiooooffffiiiilllleeee
  103.  
  104.        The exception to this are cases where an application makes
  105.        calls to the Digital Media Library error retrieval routine
  106.        _d_m_G_e_t_E_r_r_o_r(3dm) (see details below).
  107.  
  108.        The _I_R_I_S _D_i_g_i_t_a_l _M_e_d_i_a _P_r_o_g_r_a_m_m_i_n_g _G_u_i_d_e contains an
  109.        introduction to programming with the Audio File Library,
  110.        along with a description of each of its functions.
  111.  
  112.        The on-line man page _a_f_I_n_t_r_o(3dm) provides a brief overview
  113.        of the library and a list of the available procedure calls.
  114.  
  115.        There is a small set of Audio Utility routines contained
  116.        within libdmedia.so which are oftne used in conjunction with
  117.        the Audio File Library.  This set includes the function
  118.        _A_U_c_h_e_c_k_l_i_c_e_n_s_e(3dm), which allows an application to query
  119.        license availability for the Aware, Inc.
  120.        compressor/decompressor software on the current host, and
  121.        several functions that implement a generalized parameter-
  122.        value list object for use with the Audio File Library
  123.        routines _a_f_I_n_i_t_C_o_m_p_r_e_s_s_i_o_n_P_a_r_a_m_s(3dm) and
  124.        _a_f_G_e_t_C_o_m_p_r_e_s_s_i_o_n_P_a_r_a_m_s(3dm).  See the man page _A_U_p_v_n_e_w(3dm)
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.                                   - 3 -
  137.  
  138.  
  139.  
  140.        for a description of these routines.
  141.  
  142.        See the source code _a_i_f_c_c_o_n_v_e_r_t._c in the directory
  143.        /_u_s_r/_s_h_a_r_e/_s_r_c/_d_m_e_d_i_a/_s_o_u_n_d_c_o_m_m_a_n_d_s for Audio Utility
  144.        programming example.
  145.  
  146.  
  147.        9.1  _E_x_a_m_p_l_e__P_r_o_g_r_a_m_s__i_n__/_u_s_r_/_s_h_a_r_e_/_s_r_c_/_d_m_e_d_i_a_/_a_u_d_i_o
  148.  
  149.        The subsystems _d_m_e_d_i_a__d_e_v._s_r_c._t_o_o_l_s and
  150.        _d_m_e_d_i_a__d_e_v._s_r_c._e_x_a_m_p_l_e_s contain several source code examples
  151.        for programming with the Audio File Library. Executable
  152.        versions of some of these programs are included as standard
  153.        system utilities which can be installed from the
  154.        _d_m_e_d_i_a__e_o_e._s_w._t_o_o_l_s subsystem included with the IRIX 6.5 O/S
  155.        release.  However, as noted in the Changes section below,
  156.        most of these programs are now obsolete and are only
  157.        provided as example code, and their functionality has been
  158.        taken over by other programs.
  159.  
  160.        _s_f_p_l_a_y(1) and _s_f_r_e_c_o_r_d(1) are command-line programs for
  161.        playing and recording files in a variety of file formats and
  162.        compression types. The programs are built on top of the IRIS
  163.        Audio Library and the Audio File Library.  The source code
  164.        for these programs is available in
  165.        /_u_s_r/_s_h_a_r_e/_s_r_c/_d_m_e_d_i_a/_s_o_u_n_d_c_o_m_m_a_n_d_s/{_p_l_a_y._c,_s_f_r_e_c_o_r_d._c}
  166.  
  167.        _a_i_f_c_i_n_f_o(1) is a command-line program that parses an
  168.        AIFF/AIFF-C input file and prints a description of the data
  169.        in the file, including characteristics of the audio sample
  170.        data, and a summary of the nonaudio data (strings, loop
  171.        points, and so on) found in the various file header fields.
  172.        The source code is installed in
  173.        /_u_s_r/_s_h_a_r_e/_s_r_c/_d_m_e_d_i_a/_s_o_u_n_d_c_o_m_m_a_n_d_s/_a_i_f_c_i_n_f_o._c.
  174.  
  175.        _a_i_f_f_2_a_i_f_c(1), _a_i_f_c_2_a_i_f_f(1), _a_i_f_c_c_o_m_p_r_e_s_s(1), and
  176.        _a_i_f_c_d_e_c_o_m_p_r_e_s_s(1) are old command-line utility programs for
  177.        converting between AIFF-C and AIFF files, or for converting
  178.        AIFF-C files between compressed and uncompressed data
  179.        formats.  These utilities are all obsolete, as indicated
  180.        below.  The source for the program is the file
  181.        /_u_s_r/_s_h_a_r_e/_s_r_c/_d_m_e_d_i_a/_s_o_u_n_d_c_o_m_m_a_n_d_s/_a_i_f_c_c_o_n_v_e_r_t._c.
  182.  
  183.        9.2  _C_h_a_n_g_e_s__a_n_d__A_d_d_i_t_i_o_n_s__S_i_n_c_e__6_._4
  184.  
  185.        This section lists changes/additions to the Audio File
  186.        Library and accompanying example source code since its last
  187.        release.
  188.  
  189.           +o One new compression type has been added: Federal
  190.             Standard 1016 CELP.  IMA4:1 compression is now
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.                                   - 4 -
  203.  
  204.  
  205.  
  206.             available for both WAVE and AIFF-C file formats.
  207.  
  208.           +o AIFF-C files now support 32-bit single precision and
  209.             64-bit double precision floating point data formats.
  210.             Note that though this conforms to Apple, Inc.'s
  211.             specification, it is likely that very few non-SGI
  212.             applications will be equipped to read these files.
  213.  
  214.           +o AIFF-C files with audio compression now precisely
  215.             conform to Apple, Inc.'s specification.  This will
  216.             allow much greater portability for such files.
  217.  
  218.           +o The aaaaffffQQQQuuuueeeerrrryyyy(3dm) routine has been expanded to include
  219.             queries regarding compression parameters.  See the man
  220.             page for this routine for further information.
  221.  
  222.        9.3  _C_h_a_n_g_e_s__a_n_d__A_d_d_i_t_i_o_n_s__S_i_n_c_e__b_e_t_w_e_e_n__A_F__v_e_r_s_i_o_n_s__1__a_n_d__2
  223.  
  224.        This section lists changes/additions to the Audio File
  225.        Library and accompanying example source code between the old
  226.        (5.3) and the new version.  This information remains
  227.        available for developers who are upgrading directly from a
  228.        5.X release to 6.5.
  229.  
  230.           +o The version 2 AF allows transparent conversion of the
  231.             audio data in a file's track into an uncompressed
  232.             format of the application's choice.  This new format is
  233.             known as the _v_i_r_t_u_a_l format of the data.  The
  234.             application need not concern itself with the data
  235.             format in the file; it can specify the format in which
  236.             it would like to receive the data.  This also works in
  237.             the other direction:  A buffer of data in any
  238.             uncompressed format may be written out to a file
  239.             containing data in any other format (assuming the audio
  240.             file type specified supports that format).  Read the
  241.             _a_f_I_n_t_r_o(3dm) man page for a detailed explanation of
  242.             this procedure.
  243.  
  244.           +o The following list of audio utility programs has been
  245.             made obsolete by the new _d_m_c_o_n_v_e_r_t(3dm) and/or
  246.             _d_m_i_n_f_o(3dm) utilities:
  247.  
  248.             aaaaiiiiffffffff2222aaaaiiiiffffcccc,,,, aaaaiiiiffffcccc2222aaaaiiiiffffffff,,,, aaaaiiiiffffccccccccoooommmmpppprrrreeeessssssss,,,, aaaaiiiiffffccccddddeeeeccccoooommmmpppprrrreeeessssssss,,,, aaaaiiiiffffcccciiiinnnnffffoooo
  249.  
  250.             For backwards compatibility, these utilities still
  251.             exist as symbolic links to the new programs.  SGI
  252.             offers no guarantee that this will be continued in
  253.             future releases.
  254.  
  255.           +o The version 2 AF has adopted a new standard for the
  256.             naming of publically available routines.  All Audio
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.                                   - 5 -
  269.  
  270.  
  271.  
  272.             File Library routines now consist of the prefix "af"
  273.             (in small case letters) followed by any number of words
  274.             describing the function, with the first letter of each
  275.             word capitalized.  To allow existing code to run when
  276.             linked against the new library, two forms of
  277.             backwards-compatibility are built into the library:
  278.  
  279.                +o The public header file <dmedia/audiofile.h>
  280.                  contains a set of #define's which will cause the
  281.                  preprocessor to rename each occurrence of the old
  282.                  routine names in existing code when and if it is
  283.                  recompiled from source.
  284.  
  285.                +o For code which is not recompiled but which chooses
  286.                  to link against the 2.0 version of the library
  287.                  (see the section "How Does This DSO Magic Work?"
  288.                  below), a set of wrapper functions are contained
  289.                  within the library to allow the old routine symbol
  290.                  names to be resolved properly.  For ease in
  291.                  debugging and maximum efficiency, it is
  292.                  recommended that programs be recompiled to allow
  293.                  the redefines to happen, or better yet, rename the
  294.                  function calls using the new scheme.
  295.  
  296.        9.4  _B_u_g__F_i_x_e_s
  297.  
  298.        This section describes fixes made to the code since the 6.4
  299.        release.
  300.  
  301.           +o
  302.  
  303.           +o
  304.  
  305.        9.5  _K_n_o_w_n__P_r_o_b_l_e_m_s__a_n_d__W_o_r_k_a_r_o_u_n_d_s
  306.  
  307.        This section lists problems in the new Audio File Library
  308.        software and ways to work around them.
  309.  
  310.           +o AIFF-C comment chunks: the Audio File Library API does
  311.             not yet include routines for reading/writing comment
  312.             chunk data in AIFF-C files.  The library routine
  313.             _a_f_O_p_e_n_F_i_l_e(3dm) allows you to open an AIFF-C file that
  314.             contains a comment chunk, but there is no way to access
  315.             the comment chunk fields through library routines.
  316.  
  317.           +o Apple Computer proprietary compression algorithms:
  318.             several compression algorithms described in the AIFF-C
  319.             specification (ACE2, ACE8, MAC3, MAC6) are Apple
  320.             proprietary and cannot be decoded on IRIS workstations.
  321.             To avoid problems with proprietary or unsupported
  322.             compression algorithms, convert AIFF-C files so that
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.                                   - 6 -
  335.  
  336.  
  337.  
  338.             they contain uncompressed data before attempting to
  339.             transfer them between an IRIS workstation and another
  340.             audio workstation platform.
  341.  
  342.           +o Aware MultiRate codec:  An application is only allowed
  343.             to have one AFfilehandle open at a time which uses the
  344.             Aware MultiRate or Lossless compressor or decompressor.
  345.             This applies to the application's entire process share
  346.             group.  In addition, the MultiRate algorithm has a
  347.             large number of memory leaks.  Avoid opening large
  348.             numbers of MultiRate-compressed files in any single
  349.             application.  Due to the limited support which SGI
  350.             supplies for this codec, it is unlikely that this will
  351.             be fixed in the foreseeable future.
  352.  
  353.             Note:  These bugs does not apply to the SGI MPEG1
  354.                    implementation.
  355.  
  356.        9.6  _D_o_c_u_m_e_n_t_a_t_i_o_n__E_r_r_o_r_s
  357.  
  358.        This section lists errors in the Audio File Library
  359.        documentation.
  360.  
  361.           +o No known errors at this time.
  362.  
  363.  
  364.        9.7  _A_u_d_i_o__F_i_l_e__L_i_b_r_a_r_y_:__D_e_v_e_l_o_p_e_r__N_o_t_e_s
  365.  
  366.        An important warning to developers about using the IRIX 6.X
  367.        Audio File Library (version 2).  This information continues
  368.        to be provided as a reference for porting old IRIX 5.X
  369.        applications to later OS releases.  The 2.0 version of the
  370.        Audio File Library is referred to as the "new" version.
  371.  
  372.        9.7.1  _I_n_t_r_o_d_u_c_t_i_o_n  It has come to our attention that some
  373.        applications which use the Audio File Library are making
  374.        untrue assumptions about the library.  These assumptions,
  375.        though in conflict with the practice laid out by our
  376.        documentation, happened coincidentally not to cause any
  377.        errors with versions of the library shipped with IRIX 5.3
  378.        and earlier.  But it is entirely possible that some of these
  379.        mis-coded applications will fail when run with this new
  380.        release of the library.
  381.  
  382.        As the Audio File Library is a Dynamic Shared Object (DSO),
  383.        this is a matter of some concern to developers, as end-users
  384.        may upgrade OS releases and get a new DSO which breaks their
  385.        applications without the applications' developer having had
  386.        a chance to re-release.
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.                                   - 7 -
  401.  
  402.  
  403.  
  404.        The library released with IRIX 6.X was a superset of the
  405.        previously-released library.  New functionality has been
  406.        added and will continue to be added, but all previously-
  407.        existing functions are still present.
  408.  
  409.        This section is provided to warn developers of problems
  410.        which may be experienced with the new releases due to
  411.        incorrect assumptions made in application code.
  412.  
  413.        The first problem involves assumptions about the kinds of
  414.        files the library will be able to open.  The second and
  415.        third problems have to do with using the Audio Library in
  416.        multi-threaded applications.  The fourth problem involves
  417.        the use of an AFfilehandle's file descriptor while the
  418.        filehandle is alive.  Then we follow with a warning about
  419.        the correlation between AF calls and actual UNIX operations
  420.        on files (such as read(), write(), and lseek() system
  421.        calls).
  422.  
  423.        9.7.2  _U_s_e__o_f__t_h_e__O_l_d__V_e_r_s_i_o_n__o_f__t_h_e__A_u_d_i_o__F_i_l_e__L_i_b_r_a_r_y
  424.        Because the confusion described above seems to be fairly
  425.        widespread, and because some of this confusion may have
  426.        stemmed from insufficient emphasis of these points in SGI's
  427.        documentation, the new library has been modified via the SGI
  428.        DSO versioning system so that programs built prior to the
  429.        6.X release will still bind to a copy of the old library.
  430.        This means that old AF programs will not be able to get the
  431.        feature and performance improvements of the new Audio File
  432.        Library without relinking.  But this will also decrease the
  433.        chances that bugs in developers' application code such as
  434.        the ones described below will be exposed, until the
  435.        developer has a chance to repair their application, relink
  436.        to bind to the new DSO, and ship a new version to their
  437.        customers.
  438.  
  439.        We did this version change as a one-time aid for developers.
  440.        We do not intend to create new DSO versions for each new
  441.        audio file library upgrade.
  442.  
  443.        We give no guarantees that the old version of libaudiofile
  444.        will be maintained in the same manner as the new version, or
  445.        in any way whatsoever.  But we can guarantee that we will
  446.        not intentionally make changes to it which would cause
  447.        applications with the below pitfalls to fail.  Please note
  448.        however that some of the problems below could happen at any
  449.        time, whether libaudiofile changes or not.  Some could
  450.        surface to users as a result of a change of any software on
  451.        the system or even a change of system hardware.  So just
  452.        because we are keeping this older libaudiofile.so does not
  453.        mean developers have any guarantee whatsoever that their
  454.        programs will continue to execute fine.  All developers
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.                                   - 8 -
  467.  
  468.  
  469.  
  470.        should check their software for these problems and remove
  471.        them as soon as possible.
  472.  
  473.        9.7.3  _H_o_w__D_o_e_s__T_h_i_s__D_S_O__M_a_g_i_c__W_o_r_k_?  The version of the
  474.        Audio File library which shipped with IRIX 5.1, 5.2, and 5.3
  475.        was stamped with the interface version "sgi1.0".  This can
  476.        be seen by looking at the MIPS_IVERSION entry of the DSO
  477.        using "elfdump -L <library>".  Similarly, the IRIX 6.X
  478.        version of the Audio File Library is stamped "sgi2.0".  When
  479.        a program (specifically, a NON-ABI executable) is linked
  480.        under IRIX 5.X and the DSO is specified on the command line
  481.        using an argument such as -laudiofile, ld looks at the
  482.        currently available DSOs, picks the one called
  483.        libaudiofile.so, and places that DSO's name and its
  484.        interface version stamp in the executable's liblist, which
  485.        can be seen with "elfdump -Dl <executable>".
  486.  
  487.        Then at some later time, the executable is run.  This could
  488.        be before or after a new version of the Audio File Library
  489.        is installed.  The system (specifically, rld) must then find
  490.        a libaudiofile DSO to use which has a major version of 1
  491.        (the major version is the number before the decimal point in
  492.        the interface version stamp "sgi1.0").  If no new version of
  493.        the Audio File Library has been installed, the system will
  494.        immediately find libaudiofile.so, discover that it has an
  495.        interface version of "sgi1.0", and use it.  If a new version
  496.        of the Audio File Library has been installed as
  497.        libaudiofile.so, the system will find this DSO first, it
  498.        will look and see that the version number is "sgi2.0", and
  499.        it will reject this file.  It will then look for another DSO
  500.        called libaudiofile.so.1, since it wants to find major
  501.        version 1 of this DSO.  Since we have now installed the old
  502.        DSO libaudiofile.so.1, the system will find this DSO and use
  503.        it.
  504.  
  505.        In order for an old program to bind to the new DSO, the
  506.        programmer must relink it on a system which has version 2 of
  507.        the DSO.  This way, "sgi2.0" will be stored in the entry of
  508.        program's liblist for libaudiofile.so.  An alternate way to
  509.        make an AF program bind to the new DSO instead of the old
  510.        (1.0) DSO is to set the environment variable _RLD_ARGS to
  511.        '-ignore_version libaudiofile.so', but this will affect all
  512.        AF programs executed with this environment variable set.
  513.  
  514.        DSOs and DSO versioning are covered in dso(5), ld(1),
  515.        rld(1), and other man pages.  dso(5) contains references for
  516.        more information.
  517.  
  518.        9.7.4  _P_R_O_B_L_E_M__1  Assumption that AF only reads and writes a
  519.        certain set of file and data formats.
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.                                   - 9 -
  533.  
  534.  
  535.  
  536.        The AF is a library which now supports many file formats
  537.        (besides AIFF and AIFF-C) and data formats (besides 2's
  538.        complement integer and compressed data formats).
  539.  
  540.        This is why we always stated in the old man pages that
  541.        certain formats were the "currently supported" formats (man
  542.        afIntro(3dm)), or that certain data formats or chunk
  543.        configurations were possible "from AIFF files" rather than
  544.        "from files".
  545.  
  546.        The Audio File Library has the following behaviors which may
  547.        break applications making the above assumption when a new
  548.        library is released:
  549.  
  550.        int filefmt = afIdentifyFD(fd);
  551.        int filefmt = afGetFileFormat(handle, &version);
  552.  
  553.        Applications use afIdentifyFD for two reasons:  seeing
  554.        whether the AF recognizes an audio file, and seeing
  555.        specifically which format the audio file has.  The user uses
  556.        afGetFileFormat for the latter purpose (as well as getting
  557.        the format version, if applicable).  This section covers the
  558.        pitfalls of each use separately:
  559.  
  560.        CHECK FOR RECOGNITION:
  561.  
  562.        The filefmt returned could very well be something other than
  563.        AF_FILE_AIFF or AF_FILE_AIFFC.  This version of the library,
  564.        for example, supports six formats. The application should
  565.        make no assumptions about what tokens could be returned.
  566.  
  567.        Therefore code such as:
  568.  
  569.  
  570.            iiiiffff ((((ffffiiiilllleeeeffffmmmmtttt !!!!==== AAAAFFFF____FFFFIIIILLLLEEEE____AAAAIIIIFFFFFFFF &&&&&&&&
  571.               ffffiiiilllleeeeffffmmmmtttt !!!!==== AAAAFFFF____FFFFIIIILLLLEEEE____AAAAIIIIFFFFFFFFCCCC))))
  572.                   {{{{
  573.                   pppprrrriiiinnnnttttffff((((""""tttthhhhiiiissss ffffiiiilllleeee iiiissss nnnnooootttt ssssuuuuppppppppoooorrrrtttteeeedddd bbbbyyyy tttthhhheeee AAAAFFFF lllliiiibbbbrrrraaaarrrryyyy!!!!\\\\nnnn""""))));;;;
  574.                   eeeexxxxiiiitttt((((0000))));;;;
  575.                   }}}}
  576.  
  577.        is incorrect because it is quite reasonable for the AF to
  578.        return a value other than one found in the include file.
  579.        Instead, the code should test whether the AF did not
  580.        recognize the file format rather than testing for what it
  581.        thinks is all possible recognized formats. Here's one
  582.        example:
  583.  
  584.  
  585.            iiiiffff ((((ffffiiiilllleeeeffffmmmmtttt ======== AAAAFFFF____FFFFIIIILLLLEEEE____UUUUNNNNSSSSUUUUPPPPPPPPOOOORRRRTTTTEEEEDDDD ||||||||
  586.                ffffiiiilllleeeeffffmmmmtttt ======== AAAAFFFF____FFFFIIIILLLLEEEE____UUUUNNNNKKKKNNNNOOOOWWWWNNNN))))
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.                                   - 10 -
  599.  
  600.  
  601.  
  602.                    {{{{
  603.                    pppprrrriiiinnnnttttffff((((""""tttthhhhiiiissss ffffiiiilllleeee iiiissss nnnnooootttt ssssuuuuppppppppoooorrrrtttteeeedddd bbbbyyyy tttthhhheeee AAAAFFFF lllliiiibbbbrrrraaaarrrryyyy!!!!\\\\nnnn""""))));;;;
  604.                    eeeexxxxiiiitttt((((0000))));;;;
  605.                    }}}}
  606.  
  607.        The difference here is that the code acknowledges that there
  608.        are file formats which could be returned which the coder did
  609.        not know about at the time of coding.
  610.  
  611.        GETTING THE FORMAT ITSELF
  612.  
  613.        What about programs which actually care what the file format
  614.        of a file they are opening is?  Note that this should be
  615.        rare, because in the AF, the data format of a file's track
  616.        is queried and manipulated totally independently of the
  617.        file's file format.  So only programs which actually have to
  618.        make distinctions like "AIFF vs. AIFF-C" need use
  619.        afGetFileFormat or afIdentifyFD for this purpose.
  620.  
  621.        The caveat here, just as stated above, is that the library
  622.        could easily return something other than one of the
  623.        AF_FILE_... tokens found in /usr/include/audiofile.h at the
  624.        time of compilation.  This is because the Audio File Library
  625.        is a dynamic shared object (DSO), and thus when newer
  626.        versions of the audio file library supporting new file
  627.        formats are released with new OS's, programs linked at an
  628.        earlier time will automatically pick up the new library, and
  629.        thus could see the tokens corresponding to the newly
  630.        supported formats.
  631.  
  632.        Before you start worrying about how to handle this
  633.        situation, make sure you have to worry about the file format
  634.        at all:
  635.  
  636.        Many applications which check the filefmt should really be
  637.        checking the sampfmt and other track parameters of the
  638.        file's track.  For example, there is no reason why a program
  639.        which simply reads the audio data out of a 16-bit
  640.        AF_SAMPFMT_TWOSCOMP AIFF file would not be able to function
  641.        just as well reading that kind of data out of a NeXT/Sun
  642.        file, as long as it did not intend to read AIFF-specific
  643.        chunks out of it as well (certain miscs and insts, for
  644.        example).  Such a program has no need to call afIdentifyFD
  645.        or afGetFileFormat to get the file format.
  646.  
  647.        But for those applications that do care about the file
  648.        format, code such as this:
  649.  
  650.  
  651.            sssswwwwiiiittttcccchhhh ((((aaaaffffIIIIddddeeeennnnttttiiiiffffyyyyFFFFDDDD((((ffffdddd))))))))
  652.            {{{{
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.                                   - 11 -
  665.  
  666.  
  667.  
  668.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____AAAAIIIIFFFFFFFF::::
  669.                pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss iiiissss aaaannnn AAAAIIIIFFFFFFFF ffffiiiilllleeee....\\\\nnnn""""))));;;;
  670.                ddddoooo____aaaaiiiiffffffff____tttthhhhiiiinnnngggg(((())));;;;
  671.                bbbbrrrreeeeaaaakkkk;;;;
  672.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____AAAAIIIIFFFFFFFFCCCC::::
  673.                    pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss iiiissss aaaannnn AAAAIIIIFFFFFFFF----CCCC ffffiiiilllleeee....\\\\nnnn""""))));;;;
  674.                    ddddoooo____aaaaiiiiffffffffcccc____tttthhhhiiiinnnngggg(((())));;;;
  675.                    bbbbrrrreeeeaaaakkkk;;;;
  676.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____UUUUNNNNKKKKNNNNOOOOWWWWNNNN::::
  677.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____UUUUNNNNSSSSUUUUPPPPPPPPOOOORRRRTTTTEEEEDDDD::::
  678.                    pppprrrriiiinnnnttttffff((((""""tttthhhhiiiissss ffffiiiilllleeee iiiissss nnnnooootttt ssssuuuuppppppppoooorrrrtttteeeedddd bbbbyyyy AAAAFFFF lllliiiibbbbrrrraaaarrrryyyy!!!!!!!!\\\\nnnn""""))));;;;
  679.                    eeeexxxxiiiitttt((((0000))));;;;
  680.            ddddeeeeffffaaaauuuulllltttt::::
  681.                    ////**** aaaassssssssuuuummmmeeee tttthhhheeeerrrreeee hhhhaaaassss bbbbeeeeeeeennnn ssssoooommmmeeee pppprrrrooooggggrrrraaaammmmmmmmaaaattttiiiicccc eeeerrrrrrrroooorrrr ****////
  682.                    pppprrrriiiinnnnttttffff((((""""bbbbaaaadddd rrrreeeettttuuuurrrrnnnn vvvvaaaalllluuuueeee ffffrrrroooommmm AAAAFFFF lllliiiibbbbrrrraaaarrrryyyy!!!!!!!!\\\\nnnn""""))));;;;
  683.                    aaaasssssssseeeerrrrtttt((((0000))));;;;
  684.            }}}}
  685.  
  686.        should be changed to code that at least does this:
  687.  
  688.  
  689.            sssswwwwiiiittttcccchhhh ((((aaaaffffIIIIddddeeeennnnttttiiiiffffyyyyFFFFDDDD((((ffffdddd))))))))
  690.            {{{{
  691.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____AAAAIIIIFFFFFFFF::::
  692.                    pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss iiiissss aaaannnn AAAAIIIIFFFFFFFF ffffiiiilllleeee....\\\\nnnn""""))));;;;
  693.                    ddddoooo____aaaaiiiiffffffff____tttthhhhiiiinnnngggg(((())));;;;
  694.                    bbbbrrrreeeeaaaakkkk;;;;
  695.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____AAAAIIIIFFFFFFFFCCCC::::
  696.                    pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss iiiissss aaaannnn AAAAIIIIFFFFFFFF----CCCC ffffiiiilllleeee....\\\\nnnn""""))));;;;
  697.                    ddddoooo____aaaaiiiiffffffffcccc____tttthhhhiiiinnnngggg(((())));;;;
  698.                    bbbbrrrreeeeaaaakkkk;;;;
  699.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____UUUUNNNNKKKKNNNNOOOOWWWWNNNN::::
  700.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____UUUUNNNNSSSSUUUUPPPPPPPPOOOORRRRTTTTEEEEDDDD::::
  701.                    pppprrrriiiinnnnttttffff((((""""tttthhhhiiiissss ffffiiiilllleeee iiiissss nnnnooootttt ssssuuuuppppppppoooorrrrtttteeeedddd bbbbyyyy AAAAFFFF lllliiiibbbbrrrraaaarrrryyyy!!!!!!!!\\\\nnnn""""))));;;;
  702.                    eeeexxxxiiiitttt((((0000))));;;;
  703.            ddddeeeeffffaaaauuuulllltttt::::
  704.                    pppprrrriiiinnnnttttffff((((""""tttthhhhiiiissss pppprrrrooooggggrrrraaaammmm ccccaaaannnnnnnnooootttt hhhhaaaannnnddddlllleeee tttthhhhiiiissss ffffiiiilllleeee ffffoooorrrrmmmmaaaatttt!!!!\\\\nnnn""""))));;;;
  705.                    eeeexxxxiiiitttt((((0000))));;;;
  706.            }}}}
  707.  
  708.        Applications can now query the AF at runtime in order to
  709.        determine which file formats are supported by the version of
  710.        the AF to which the running program has bound.  In addition
  711.        to querying the token that is used to refer to each file
  712.        format, users can query for the textual name and other
  713.        characteristics of each format.  See the _a_f_Q_u_e_r_y(3dm) man
  714.        page for a complete description of this technique.
  715.  
  716.        For many applications, getting the textual name of the file
  717.        format is the entire reason they wanted to call afIdentifyFD
  718.        or afGetFileFormat in the first place, and so such
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.                                   - 12 -
  731.  
  732.  
  733.  
  734.        applications can be now written so that they instantly and
  735.        automatically understand every file format which the
  736.        currently-bound Audio File Library understands, as in:
  737.  
  738.  
  739.            iiiinnnntttt ffffiiiilllleeeeffffmmmmtttt ==== aaaaffffIIIIddddeeeennnnttttiiiiffffyyyyFFFFDDDD((((ffffdddd))));;;;
  740.            cccchhhhaaaarrrr ****nnnnaaaammmmeeee ==== ((((cccchhhhaaaarrrr ****)))) aaaaffffQQQQuuuueeeerrrryyyyPPPPooooiiiinnnntttteeeerrrr((((AAAAFFFF____QQQQUUUUEEEERRRRYYYYTTTTYYYYPPPPEEEE____FFFFIIIILLLLEEEEFFFFMMMMTTTT,,,,
  741.                                                 AAAAFFFF____QQQQUUUUEEEERRRRYYYY____NNNNAAAAMMMMEEEE,,,, ffffiiiilllleeeeffffmmmmtttt,,,, 0000,,,, 0000))));;;;
  742.            pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss iiiissss ffffiiiilllleeee hhhhaaaassss tttthhhheeee %%%%ssss ffffiiiilllleeee ffffoooorrrrmmmmaaaatttt....\\\\nnnn"""",,,, nnnnaaaammmmeeee))));;;;
  743.  
  744.        So what guarantees does the developer have?
  745.  
  746.        The library will never support fewer formats.  If a user
  747.        uses an AF_FILE_ token that we ship with a software release,
  748.        that token and its associated file format will never become
  749.        unsupported, although it may become obsolete (and perhaps
  750.        new features will not be available for it).  So for example,
  751.        the library will never stop supporting AF_FILE_AIFF or
  752.        AF_FILE_AIFFC.
  753.  
  754.        The library will always provide the query mechanism.  We
  755.        have made every effort to make the query mechanism versatile
  756.        enough that user programs can make intelligent decisions
  757.        about how to deal with and report info on audio files of
  758.        different file formats.
  759.  
  760.        afGetSampleFormat(AFfilehandle, int track, int *sampfmt, int
  761.        *sampwidth);
  762.  
  763.        Here is the single most likely offender that could be found
  764.        in developer's programs.  Because the new AF supports
  765.        transparent data format conversion, you may not have to use
  766.        this routine at all, and many of the below-mentioned issues
  767.        will go away.
  768.  
  769.        As the man page says, AIFF and AIFF-C files only support
  770.        AF_SAMPFMT_TWOSCOMP files.  However, as the library now
  771.        support other file formats, it is quite likely the library
  772.        may return some other sample format.  So code that says:
  773.  
  774.  
  775.            AAAAFFFFffffiiiilllleeeehhhhaaaannnnddddlllleeee hhhh ==== aaaaffffOOOOppppeeeennnnFFFFiiiilllleeee((((................))));;;;
  776.            iiiiffff ((((!!!!hhhh)))) rrrreeeettttuuuurrrrnnnn;;;;
  777.  
  778.            aaaaffffGGGGeeeettttSSSSaaaammmmpppplllleeeeFFFFoooorrrrmmmmaaaatttt((((hhhh,,,, AAAAFFFF____DDDDEEEEFFFFAAAAUUUULLLLTTTT____TTTTRRRRAAAACCCCKKKK,,,, &&&&ssssaaaammmmppppffffmmmmtttt,,,, &&&&ssssaaaammmmppppwwwwiiiiddddtttthhhh))));;;;
  779.  
  780.            ////**** ssssaaaammmmppppffffmmmmtttt ccccoooouuuulllldddd oooonnnnllllyyyy bbbbeeee AAAAFFFF____SSSSAAAAMMMMPPPPFFFFMMMMTTTT____TTTTWWWWOOOOSSSSCCCCOOOOMMMMPPPP--------nnnnoooo nnnneeeeeeeedddd ttttoooo cccchhhheeeecccckkkk!!!! ****////
  781.  
  782.            sssswwwwiiiittttcccchhhh ((((ssssaaaammmmppppwwwwiiiiddddtttthhhh))))
  783.                    {{{{
  784.                    ............ ////**** sssseeeeeeee bbbbeeeelllloooowwww ffffoooorrrr nnnnooootttteeee oooonnnn tttthhhhiiiissss ppppaaaarrrrtttt ****////
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.                                   - 13 -
  797.  
  798.  
  799.  
  800.                    }}}}
  801.  
  802.        is incorrect.  It is by no means guaranteed that sampfmt is
  803.        two's complement.  Nor is it guaranteed that sampwidth has
  804.        any meaning--the meaning of sampwidth depends on sampfmt.
  805.  
  806.        For example, this version of the Audio File Library can
  807.        transfer IEEE floats and IEEE doubles.  For these types,
  808.        sampwidth has no meaning, and it is ignored and unset.  The
  809.        code above would behave very unpredictably.
  810.  
  811.        Code should say:
  812.  
  813.  
  814.            AAAAFFFFffffiiiilllleeeehhhhaaaannnnddddlllleeee hhhh ==== aaaaffffOOOOppppeeeennnnFFFFiiiilllleeee((((................))));;;;
  815.            iiiiffff ((((!!!!hhhh)))) rrrreeeettttuuuurrrrnnnn;;;;
  816.  
  817.            aaaaffffGGGGeeeettttSSSSaaaammmmpppplllleeeeFFFFoooorrrrmmmmaaaatttt((((hhhh,,,, AAAAFFFF____DDDDEEEEFFFFAAAAUUUULLLLTTTT____TTTTRRRRAAAACCCCKKKK,,,, &&&&ssssaaaammmmppppffffmmmmtttt,,,, &&&&ssssaaaammmmppppwwwwiiiiddddtttthhhh))));;;;
  818.  
  819.            iiiiffff ((((ssssaaaammmmppppffffmmmmtttt !!!!==== AAAAFFFF____SSSSAAAAMMMMPPPPFFFFMMMMTTTT____TTTTWWWWOOOOSSSSCCCCOOOOMMMMPPPP))))
  820.                    {{{{
  821.                    pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss pppprrrrooooggggrrrraaaammmm ccccaaaannnn''''tttt rrrreeeeaaaadddd aaaauuuuddddiiiioooo ffffiiiilllleeeessss ooooffff tttthhhhiiiissss """"
  822.                           ssssaaaammmmpppplllleeee ffffoooorrrrmmmmaaaatttt\\\\nnnn""""))));;;;
  823.                    eeeexxxxiiiitttt((((0000))));;;;
  824.                    }}}}
  825.  
  826.            sssswwwwiiiittttcccchhhh ((((ssssaaaammmmppppwwwwiiiiddddtttthhhh)))) ////**** nnnnoooowwww wwwweeee kkkknnnnoooowwww ssssaaaammmmppppwwwwiiiiddddtttthhhh iiiissss mmmmeeeeaaaannnniiiinnnnggggffffuuuullll ****////
  827.                    {{{{
  828.                    ............ ////**** sssseeeeeeee bbbbeeeelllloooowwww ffffoooorrrr nnnnooootttteeee oooonnnn tttthhhhiiiissss ppppaaaarrrrtttt ****////
  829.                    }}}}
  830.  
  831.        Another note on sampwidth: even with AIFF files, the sample
  832.        width is not necessarily a multiple of 8.  Generally, this
  833.        can be ignored, because audio samples which do not take up
  834.        an integral number of bytes are left-justified inside the
  835.        next larger integral number of bytes (with the remaining
  836.        bits set to 0).  But this may be an issue in code such as
  837.        the switch statement above, if it were written:
  838.  
  839.  
  840.            sssswwwwiiiittttcccchhhh ((((ssssaaaammmmppppwwwwiiiiddddtttthhhh)))) ////**** nnnnoooowwww ssssaaaammmmppppwwwwiiiiddddtttthhhh iiiissss mmmmeeeeaaaannnniiiinnnnggggffffuuuullll ****////
  841.                    {{{{
  842.                    ccccaaaasssseeee 8888::::  ddddoooo____8888____tttthhhhiiiinnnngggg(((())));;;;  bbbbrrrreeeeaaaakkkk;;;;
  843.                    ccccaaaasssseeee 11116666:::: ddddoooo____11116666____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  844.                    ccccaaaasssseeee 22224444:::: ddddoooo____22224444____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  845.                    ccccaaaasssseeee 33332222:::: ddddoooo____33332222____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  846.                    ddddeeeeffffaaaauuuulllltttt::::
  847.                            ////**** ffffaaaallllsssseeee aaaassssssssuuuummmmppppttttiiiioooonnnn ****////
  848.                            pppprrrriiiinnnnttttffff((((""""bbbbaaaadddd rrrreeeettttuuuurrrrnnnn vvvvaaaalllluuuueeee ffffrrrroooommmm AAAAFFFF lllliiiibbbbrrrraaaarrrryyyy!!!!\\\\nnnn""""))));;;;
  849.                            aaaasssssssseeeerrrrtttt((((0000))));;;;
  850.                    }}}}
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.                                   - 14 -
  863.  
  864.  
  865.  
  866.        There are two problems here.  One is that the code assumes
  867.        the maximum size of samples in files opened by the library
  868.        is 32 bits, which is definitely not true.  The second false
  869.        assumption is that the number of bits will be a multiple of
  870.        8.  Better code would be (after checking that sampfmt is an
  871.        integer format):
  872.  
  873.  
  874.            ////**** rrrroooouuuunnnndddd ssssaaaammmmppppwwwwiiiiddddtttthhhh uuuupppp ttttoooo nnnneeeeaaaarrrreeeesssstttt nnnnuuuummmmbbbbeeeerrrr ooooffff bbbbyyyytttteeeessss ****////
  875.            iiiinnnntttt nnnnbbbbyyyytttteeeessss ==== (((( ((((ssssaaaammmmppppwwwwiiiiddddtttthhhh----1111)))) //// 8888 )))) ++++ 1111;;;;
  876.            sssswwwwiiiittttcccchhhh ((((nnnnbbbbyyyytttteeeessss))))
  877.                    {{{{
  878.                    ccccaaaasssseeee 1111::::  ddddoooo____8888____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  879.                    ccccaaaasssseeee 2222:::: ddddoooo____11116666____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  880.                    ccccaaaasssseeee 3333:::: ddddoooo____22224444____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  881.                    ccccaaaasssseeee 4444:::: ddddoooo____33332222____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  882.                    ddddeeeeffffaaaauuuulllltttt::::
  883.                            pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss pppprrrrooooggggrrrraaaammmm ccccaaaannnn''''tttt rrrreeeeaaaadddd aaaauuuuddddiiiioooo ffffiiiilllleeeessss ooooffff tttthhhhiiiissss """"
  884.                                   ssssaaaammmmpppplllleeee wwwwiiiiddddtttthhhh %%%%dddd\\\\nnnn"""",,,, ssssaaaammmmppppwwwwiiiiddddtttthhhh))));;;;
  885.                            eeeexxxxiiiitttt((((0000))));;;;
  886.                    }}}}
  887.  
  888.        One final thing about sample widths--don't forget that there
  889.        is a special case for reading 24-bit integer data.  Because
  890.        3-byte data is so difficult to deal with, the library
  891.        automatically stretches the data out into convenient 4-byte
  892.        quantities in a way compatible with the SGI Audio Library
  893.        (AL).  The tricky thing here is that the data is sign-
  894.        extended on the left for this special case of 17-24-bit
  895.        data, whereas the padding that goes on to handle non-
  896.        multiple-of-8 sample widths is done by zero-bit-padding on
  897.        the right.  Here are some examples which should make this
  898.        clear, for the case of AF_SAMPFMT_TWOSCOMP:
  899.  
  900.  
  901.        MMMMSSSSBBBB                          LLLLSSSSBBBB
  902.        bbbbyyyytttteeee 3333  bbbbyyyytttteeee 2222  bbbbyyyytttteeee 1111  bbbbyyyytttteeee 0000
  903.        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddd |||| SSSSaaaammmmpppplllleeee FFFFoooorrrrmmmmaaaatttt
  904.        ------------------------------------------------------------------------------------------------------------------------------------++++--------------------------------------------------------------------------------------------------------------------------------
  905.                                dddddddddddddddddddddddd00000000 |||| 6666----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 8888 bbbbiiiittttssss
  906.                                      RRRRRRRR ||||  ((((rrrriiiigggghhhhtttt ppppaaaadddd))))
  907.                                         ||||
  908.                                dddddddddddddddddddddddddddddddd |||| 8888----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 8888 bbbbiiiittttssss
  909.                                         ||||  ((((nnnnoooo ppppaaaaddddddddiiiinnnngggg))))
  910.                                         ||||
  911.                        ccccccccccccccccccccccccccccccccdddddddddddddddd0000000000000000 |||| 11112222----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 11116666 bbbbiiiittttssss
  912.                                    RRRRRRRRRRRRRRRR ||||  ((((rrrriiiigggghhhhtttt ppppaaaadddd))))
  913.                                         ||||
  914.                        ccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddd |||| 11116666----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 11116666 bbbbiiiittttssss
  915.                                         ||||  ((((nnnnoooo ppppaaaaddddddddiiiinnnngggg))))
  916.                                         ||||
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.                                   - 15 -
  929.  
  930.  
  931.  
  932.        ssss<<<<------------------------bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccdddddddddddddddd0000000000000000 |||| 22220000----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 33332222 bbbbiiiittttssss
  933.        SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS                    RRRRRRRRRRRRRRRR ||||  ((((lllleeeefffftttt ssssiiiiggggnnnn----eeeexxxxtttteeeennnndddd AAAANNNNDDDD rrrriiiigggghhhhtttt ppppaaaadddd !!!!))))
  934.                                         ||||
  935.        ssss<<<<------------------------bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddd |||| 22224444----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 33332222 bbbbiiiittttssss
  936.        SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS                         ||||  ((((lllleeeefffftttt ssssiiiiggggnnnn----eeeexxxxtttteeeennnndddd))))
  937.                                         ||||
  938.        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccdddddddddddddddddddddddd00000000 |||| 33330000----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 33332222 bbbbiiiittttssss
  939.                                      RRRRRRRR ||||  ((((rrrriiiigggghhhhtttt ppppaaaadddd))))
  940.                                         ||||
  941.        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddd |||| 33332222----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 33332222 bbbbiiiittttssss
  942.                                         ||||  ((((nnnnoooo ppppaaaaddddddddiiiinnnngggg))))
  943.                                         ||||
  944.  
  945.        Unlike file formats, it doesn't make sense to have a runtime
  946.        query of all possible sample formats.  The existence of the
  947.        new virtual audio format system described above allows
  948.        applications to specify in which format they wish to receive
  949.        data.
  950.  
  951.        int afGetCompression(AFfilehandle, int);
  952.        void afGetCompressionParams(AFfilehandle, int, int *, AUpvlist);
  953.  
  954.        Obviously, new compression methods will be supported, so the
  955.        same warnings that apply to afGetSampleFormat apply here.
  956.  
  957.        As the library transparently decompresses input data and
  958.        compresses output data, this is not such a big issue.  The
  959.        application simply must specify the virtual format desired.
  960.  
  961.        A given already-released codec in our library will not
  962.        change its native sample format, if we have specified what
  963.        that codec's native sample format is.  So, for example, if
  964.        you know the file is AF_COMPRESSION_G711_ULAW, then you can
  965.        assume the native format for that codec is
  966.        AF_SAMPFMT_TWOSCOMP, 16 because we said this in a man page.
  967.        This is for backwards compatibility. But we do not guarantee
  968.        that all future codecs will generate 16-bit signed integer
  969.        data natively, nor do we guarantee that all future codecs
  970.        will have a single native sample format (some may be
  971.        configurable or may vary depending on what kind of data was
  972.        originally compressed).  In every case though, the
  973.        programmer can call afGetSampleFormat to retrieve the sample
  974.        format for the current codec on the file corresponding to
  975.        the given AFfilehandle.
  976.  
  977.        This new version of the library has a runtime query of all
  978.        of the supported compression methods, their textual names,
  979.        their compression ratios (if available), and any other info
  980.        that may be useful in making a runtime choice of codec.
  981.        This is done via afQuery(3dm) in a similar manner to the
  982.        file format name string query above.
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.                                   - 16 -
  995.  
  996.  
  997.  
  998.        int afGetChannels(AFfilehandle, int/*track*/);
  999.  
  1000.        May return any number of channels, just like it could even
  1001.        with AIFF and AIFF-C.  The virtual number of channels may be
  1002.        set to any nonzero value.
  1003.  
  1004.        double afGetRate(AFfilehandle, int/*track*/);
  1005.  
  1006.        May return any sampling rate, just like it could even with
  1007.        AIFF and AIFF-C.  The current version of the library
  1008.        provides a transparent conversion mechanism which allows the
  1009.        programmer to deal with files with unexpected sampling rates
  1010.        in a simple and clean way via virtual format conversion.
  1011.        See _a_f_S_e_t_V_i_r_t_u_a_l_R_a_t_e(3dm) for more information.
  1012.  
  1013.        int afGetTrackIDs(AFfilehandle, int */*track ids*/);
  1014.  
  1015.        May return more than one track.  We give no guarantees that
  1016.        only one-track formats are supported.  An application might
  1017.        want to reject such a file, or perhaps use the first track.
  1018.        We do not define the correct behavior.
  1019.  
  1020.        int afGetMarkIDs(AFfilehandle, int/*track*/, int */*ids*/);
  1021.        int afGetMarkPosition(AFfilehandle, int/*track*/, int/*markid*/);
  1022.        char *afGetMarkName(AFfilehandle, int, int);
  1023.  
  1024.        May return any configuration or number of marks just as it
  1025.        may with an AIFF/AIFF-C file.
  1026.  
  1027.        Apps should be written to expect and ignore marks they do
  1028.        not understand.
  1029.  
  1030.        int afGetMiscIDs(AFfilehandle, int * /*idlist*/);
  1031.        int afGetMiscType(AFfilehandle, int /*miscid*/);
  1032.        int afGetMiscSize(AFfilehandle, int /*miscid*/);
  1033.  
  1034.        May return ANY type of misc chunk.
  1035.  
  1036.        Apps should be written to expect and ignore miscellaneous
  1037.        chunks they do not understand.
  1038.  
  1039.        Apps should be especially careful not to copy misc chunks
  1040.        from one file to another unless they understand the content
  1041.        of those misc chunks;  the chunk may contain references to
  1042.        other parts of the file which the application has modified.
  1043.        In this case the chunk in the new file becomes corrupt.
  1044.        This is to be avoided.  The chunk should not be copied and
  1045.        the user should probably be warned.
  1046.  
  1047.        int afGetAESChannelData(AFfilehandle,int,unsigned char buf[24]);
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.                                   - 17 -
  1061.  
  1062.  
  1063.  
  1064.        Will always return data that is to be interpreted as AES
  1065.        channel data, but there is never any guarantee that such
  1066.        data will be present or not in a given file format (unless
  1067.        that file format itself defines AES data as always being
  1068.        present).
  1069.  
  1070.        int afGetInstIDs(AFfilehandle, int */*inst ids*/);
  1071.        long afGetInstParamLong(AFfilehandle, int/*inst*/, int /*param*/);
  1072.  
  1073.        May return more than one inst, unlike the one fixed INST in
  1074.        AIFF/AIFF-C files.  There are many supported file formats
  1075.        which have different inst configurations than AIFF/AIFF-C.
  1076.  
  1077.        The _a_f_Q_u_e_r_y mechanism allows an application to determine in
  1078.        advance the number of inst chunks supported by a particular
  1079.        file format.  Note that this is the allowable number for
  1080.        creating a file, not an indication of how many chunks are
  1081.        present in a file being read.  _a_f_G_e_t_I_n_s_t_I_D_s _M_U_S_T be used to
  1082.        determine the number present in a file opened for read
  1083.        access prior to loading the values into an array.  This is
  1084.        very important.  The following code will dump core when
  1085.        reading some of the currently supported file formats:
  1086.  
  1087.  
  1088.            AAAAFFFFffffiiiilllleeeehhhhaaaannnnddddlllleeee hhhh ==== aaaaffffOOOOppppeeeennnnFFFFiiiilllleeee((((................))));;;;
  1089.            iiiinnnntttt iiiinnnnssssttttIIIIDDDD;;;;  ////**** iiiinnnnccccoooorrrrrrrreeeecccctttt aaaassssssssuuuummmmppppttttiiiioooonnnn tttthhhhaaaatttt tttthhhheeeerrrreeee iiiissss oooonnnnllllyyyy oooonnnneeee iiiinnnnsssstttt IIIIDDDD!!!! ****////
  1090.            iiiiffff ((((!!!!hhhh)))) rrrreeeettttuuuurrrrnnnn;;;;
  1091.            aaaaffffGGGGeeeettttIIIInnnnssssttttIIIIDDDDssss((((hhhh,,,, AAAAFFFF____DDDDEEEEFFFFAAAAUUUULLLLTTTT____TTTTRRRRAAAACCCCKKKK,,,, &&&&iiiinnnnssssttttIIIIDDDD))));;;; ////**** CCCCOOOORRRREEEE DDDDUUUUMMMMPPPP!!!!!!!! ****////
  1092.  
  1093.        The correct code should say:
  1094.  
  1095.  
  1096.            iiiinnnntttt ****iiiinnnnssssttttIIIIDDDDssss ==== NNNNUUUULLLLLLLL;;;;  ////**** ffffoooorrrr aaaarrrrrrrraaaayyyy ttttoooo----bbbbeeee----aaaallllllllooooccccaaaatttteeeedddd ****////
  1097.            iiiinnnntttt nnnnuuuummmmIIIIDDDDSSSS;;;;
  1098.            AAAAFFFFffffiiiilllleeeehhhhaaaannnnddddlllleeee hhhh ==== aaaaffffOOOOppppeeeennnnFFFFiiiilllleeee((((................))));;;;
  1099.            iiiiffff ((((!!!!hhhh)))) rrrreeeettttuuuurrrrnnnn;;;;
  1100.  
  1101.            nnnnuuuummmmIIIIDDDDssss ==== aaaaffffGGGGeeeettttIIIInnnnssssttttIIIIDDDDssss((((hhhh,,,, AAAAFFFF____DDDDEEEEFFFFAAAAUUUULLLLTTTT____TTTTRRRRAAAACCCCKKKK,,,, NNNNUUUULLLLLLLL))));;;; ////**** cccchhhheeeecccckkkk nnnnuuuummmmbbbbeeeerrrr FFFFIIIIRRRRSSSSTTTT ****////
  1102.  
  1103.            iiiiffff((((nnnnuuuummmmIIIIDDDDssss >>>> 0000)))) {{{{
  1104.                iiiinnnnssssttttIIIIDDDDssss ==== ((((iiiinnnntttt ****)))) mmmmaaaalllllllloooocccc((((nnnnuuuummmmIIIIDDDDssss **** ssssiiiizzzzeeeeooooffff((((iiiinnnntttt))))))));;;; ////**** aaaallllllllooooccccaaaatttteeee aaaarrrrrrrraaaayyyy ****////
  1105.                iiiiffff((((!!!!iiiinnnnssssttttIIIIDDDDssss)))) {{{{
  1106.                    eeeerrrrrrrroooorrrr(((())));;;;
  1107.                    rrrreeeettttuuuurrrrnnnn;;;;
  1108.                }}}}
  1109.  
  1110.                aaaaffffGGGGeeeettttIIIInnnnssssttttIIIIDDDDssss((((hhhh,,,, AAAAFFFF____DDDDEEEEFFFFAAAAUUUULLLLTTTT____TTTTRRRRAAAACCCCKKKK,,,, iiiinnnnssssttttIIIIDDDDssss))));;;; ////**** nnnnoooowwww llllooooaaaadddd IIIIDDDD aaaarrrrrrrraaaayyyy ****////
  1111.  
  1112.                ////**** ddddoooo wwwwhhhhaaaatttteeeevvvveeeerrrr wwwwiiiitttthhhh iiiinnnnssssttttIIIIDDDDssss ****////
  1113.  
  1114.                ffffrrrreeeeeeee((((iiiinnnnssssttttIIIIDDDDssss))));;;;  ////**** ffffrrrreeeeeeee tttthhhheeee mmmmeeeemmmmoooorrrryyyy ****////
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.                                   - 18 -
  1127.  
  1128.  
  1129.  
  1130.            }}}}
  1131.  
  1132.        Apps should be written to expect and ignore instrument
  1133.        configurations or instrument parameters they do not
  1134.        understand.
  1135.  
  1136.        int afGetLoopIDs(AFfilehandle, int /*inst*/, int*);
  1137.        int afGetLoopMode(AFfilehandle, int/*inst*/, int /*loopid */);
  1138.        int afGetLoopStart(AFfilehandle, int/*inst*/, int/*loopid */);
  1139.        int afGetLoopEnd(AFfilehandle, int/*inst*/, int/*loopid*/);
  1140.        int afGetLoopTrack(AFfilehandle, int/*inst*/, int/*loopid*/);
  1141.  
  1142.        May return any configurations of loops within an inst, not
  1143.        just the fixed value of 2 we have in AIFF/AIFF-C files.
  1144.        There are many supported file formats which have different
  1145.        loop configurations than AIFF/AIFF-C.
  1146.  
  1147.        The _a_f_Q_u_e_r_y mechanism allows an application to determine in
  1148.        advance the number of loops per inst supported by a
  1149.        particular file format. _a_f_G_e_t_L_o_o_p_I_D_s _M_U_S_T be used to
  1150.        determine the number present in a file opened for read
  1151.        access prior to loading the values into an array.  See the
  1152.        above note for afGetInstIDs for details.
  1153.  
  1154.        Apps should be written to expect and ignore loop
  1155.        configurations they do not understand.
  1156.  
  1157.        9.7.5  _P_R_O_B_L_E_M__2  Some developers are writing bad multi-
  1158.        threaded code that just happens to work with the AF library.
  1159.  
  1160.        We have caught several different apps trying to do the
  1161.        following from multi-threaded code and we imagine that there
  1162.        are other apps that do this as well:
  1163.  
  1164.  
  1165.        ================================ TTTThhhhrrrreeeeaaaadddd 1111 ========================================================||||============================================ TTTThhhhrrrreeeeaaaadddd 2222 ============================================
  1166.              ||||                         ||||          ||||
  1167.              |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee     ||||          |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee
  1168.              ||||  nnnnoooo sssseeeemmmmaaaapppphhhhoooorrrreeee lllloooocccckkkkiiiinnnngggg   ||||          |||| nnnnoooo sssseeeemmmmaaaapppphhhhoooorrrreeee lllloooocccckkkkiiiinnnngggg
  1169.              ||||                         ||||          ||||
  1170.                                        ||||
  1171.        aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee1111))));;;;    ||||    aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee2222))));;;;
  1172.        aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;      ||||    aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;
  1173.                                        ||||
  1174.              ||||                         ||||          ||||
  1175.              |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee     ||||          |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee
  1176.              ||||  nnnnoooo sssseeeemmmmaaaapppphhhhoooorrrreeee lllloooocccckkkkiiiinnnngggg   ||||          |||| nnnnoooo sssseeeemmmmaaaapppphhhhoooorrrreeee lllloooocccckkkkiiiinnnngggg
  1177.              ||||                         ||||          ||||
  1178.  
  1179.        Both threads are making calls on the same AFfilehandle at
  1180.        the same time, and no concern is put into the order in which
  1181.  
  1182.  
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.                                   - 19 -
  1193.  
  1194.  
  1195.  
  1196.        these calls end up being made on the filehandle.
  1197.  
  1198.        This code is just plain old flat out wrong, it doesn't
  1199.        matter if you're using the AF or any other library.
  1200.  
  1201.        It is entirely possible that the order the calls get
  1202.        executed in by the UNIX scheduler is:
  1203.  
  1204.  
  1205.        aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee1111))));;;;    ||||
  1206.                                        ||||    aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee2222))));;;;
  1207.        aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;      ||||
  1208.                                        ||||    aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;
  1209.  
  1210.        in which case both threads would read the wrong data. UNIX
  1211.        offers absolutely, positively no guarantee that this will
  1212.        not happen unless the programmer uses one of the many
  1213.        available process coordination facilities such as
  1214.        semaphores.
  1215.  
  1216.        Furthermore, no amount of work on the part of the AF library
  1217.        writers can prevent this situation, because the library has
  1218.        no knowledge of what order the programmer really wants the
  1219.        operations to come in.  We cannot use the pid as a hint
  1220.        because some correctly coded programs may want to seek in
  1221.        one thread and read in another (correctly coded programs use
  1222.        locking to make sure that these operations happen in the
  1223.        right order).
  1224.  
  1225.        Note that although our example only uses the functions
  1226.        afSeekFrame() and afReadFrames(), this problem can exist
  1227.        with calls to ANY Audio File Library functions, even ones
  1228.        which do not directly manipulate audio files (such as
  1229.        operations on an AFfilesetup or operations on an
  1230.        AFfilehandle that query or set parameters stored in memory).
  1231.        In particular, watch out for uncoordinated calls of
  1232.        afSyncFile() and afCloseFile().  Making these calls from
  1233.        more than one thread simultaneously in an uncoordinated
  1234.        fashion seems to be a common error.
  1235.  
  1236.        Correct code should look like this:
  1237.  
  1238.  
  1239.        ================================ TTTThhhhrrrreeeeaaaadddd 1111 ========================================================||||============================================ TTTThhhhrrrreeeeaaaadddd 2222 ============================================
  1240.                                        ||||
  1241.              ||||                         ||||          ||||
  1242.              |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee     ||||          |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee
  1243.              ||||                         ||||          ||||
  1244.              ||||                         ||||          ||||
  1245.                                        ||||
  1246.        LLLLoooocccckkkk SSSSeeeemmmmaaaapppphhhhoooorrrreeee tttthhhhaaaatttt gggguuuuaaaarrrrddddssss hhhh    ||||    LLLLoooocccckkkk SSSSeeeemmmmaaaapppphhhhoooorrrreeee tttthhhhaaaatttt gggguuuuaaaarrrrddddssss hhhh
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.                                   - 20 -
  1259.  
  1260.  
  1261.  
  1262.        aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee1111))));;;;    ||||    aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee2222))));;;;
  1263.        aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;      ||||    aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;
  1264.        UUUUnnnnlllloooocccckkkk SSSSeeeemmmmaaaapppphhhhoooorrrreeee tttthhhhaaaatttt gggguuuuaaaarrrrddddssss hhhh  ||||    UUUUnnnnlllloooocccckkkk sssseeeemmmmaaaapppphhhhoooorrrreeee tttthhhhaaaatttt gggguuuuaaaarrrrddddssss hhhh
  1265.                                        ||||
  1266.              ||||                         ||||          ||||
  1267.              |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee     ||||          |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee
  1268.              ||||                         ||||          ||||
  1269.              ||||                         ||||          ||||
  1270.                                        ||||
  1271.  
  1272.        The key thing to realize here is: UNIX guarantees that only
  1273.        one of the Lock Semaphore calls will succeed immediately.
  1274.        The thread whose lock does not succeed will sit and wait in
  1275.        the Lock Semaphore call (and thus not proceed to the
  1276.        afSeekFrame) until the other thread has unlocked the
  1277.        semaphore (after doing its seek AND its read).  Then when
  1278.        the other thread unlocks the semaphore, the thread who was
  1279.        forced to wait will now proceed.
  1280.  
  1281.        How does one use these semaphores?  One uses code such as
  1282.        the following to create a semaphore FileOpSema with value 1:
  1283.  
  1284.  
  1285.        ####iiiinnnncccclllluuuuddddeeee <<<<uuuulllloooocccckkkkssss....hhhh>>>>
  1286.  
  1287.        AAAAFFFFffffiiiilllleeeehhhhaaaannnnddddlllleeee hhhh;;;;      ////**** gggglllloooobbbbaaaallll ffffiiiilllleeee hhhhaaaannnnddddlllleeee ****////
  1288.        uuuusssseeeemmmmaaaa____tttt ****FFFFiiiilllleeeeOOOOppppSSSSeeeemmmmaaaa;;;; ////**** gggglllloooobbbbaaaallll sssseeeemmmmaaaapppphhhhoooorrrreeee ttttoooo pppprrrrooootttteeeecccctttt hhhh ****////
  1289.  
  1290.        ////**** iiiinnnniiiittttiiiiaaaalllliiiizzzzeeee sssseeeemmmmaaaapppphhhhoooorrrreeee ssssuuuuppppppppoooorrrrtttt -------- ddddoooo tttthhhhiiiissss oooonnnncccceeee ****////
  1291.            {{{{
  1292.            uuuussssppppttttrrrr____tttt ****uuuussssppppttttrrrr;;;;
  1293.            cccchhhhaaaarrrr ****aaaarrrreeeennnnaaaaffffiiiilllleeee;;;;
  1294.  
  1295.            ////**** uuuusssseeee ffffaaaasssstttteeeesssstttt ((((nnnnoooo ddddeeeebbbbuuuuggggggggiiiinnnngggg)))) ffffoooorrrrmmmm ooooffff sssseeeemmmmaaaapppphhhhoooorrrreeeessss -------- uuuussssccccoooonnnnffffiiiigggg ((((3333PPPP)))) ****////
  1296.            uuuussssccccoooonnnnffffiiiigggg((((CCCCOOOONNNNFFFF____LLLLOOOOCCCCKKKKTTTTYYYYPPPPEEEE,,,, UUUUSSSS____NNNNOOOODDDDEEEEBBBBUUUUGGGG))));;;;
  1297.  
  1298.            ////**** ccccrrrreeeeaaaatttteeee sssshhhhaaaarrrreeeedddd aaaarrrreeeennnnaaaa ttttoooo hhhhoooolllldddd tttthhhheeee sssseeeemmmmaaaapppphhhhoooorrrreeee -------- uuuussssiiiinnnniiiitttt((((3333PPPP)))) ****////
  1299.            aaaarrrreeeennnnaaaaffffiiiilllleeee ==== ttttmmmmppppnnnnaaaammmm((((NNNNUUUULLLLLLLL))));;;;
  1300.            uuuussssppppttttrrrr ==== uuuussssiiiinnnniiiitttt((((aaaarrrreeeennnnaaaaffffiiiilllleeee))));;;;
  1301.  
  1302.            ////****
  1303.                ccccrrrreeeeaaaatttteeee tttthhhheeee sssseeeemmmmaaaapppphhhhoooorrrreeee iiiinnnn tttthhhhaaaatttt aaaarrrreeeennnnaaaa -------- uuuussssnnnneeeewwwwsssseeeemmmmaaaa((((3333PPPP))))
  1304.                ccccrrrreeeeaaaatttteeee wwwwiiiitttthhhh ccccoooouuuunnnntttt 1111--------tttthhhheeeerrrreeee iiiissss 1111 rrrreeeessssoooouuuurrrrcccceeee ((((hhhh)))),,,, wwwwhhhhiiiicccchhhh iiiissss
  1305.                iiiinnnniiiittttiiiiaaaallllllllyyyy aaaavvvvaaaaiiiillllaaaabbbblllleeee....
  1306.            ****////
  1307.            FFFFiiiilllleeeeOOOOppppSSSSeeeemmmmaaaa ==== uuuussssnnnneeeewwwwsssseeeemmmmaaaa((((uuuussssppppttttrrrr,,,,1111))));;;;
  1308.  
  1309.            ////**** wwwweeee wwwwoooonnnn''''tttt nnnneeeeeeeedddd ttttoooo rrrreeeeffffeeeerrrr ttttoooo aaaarrrreeeennnnaaaa aaaaggggaaaaiiiinnnn ssssoooo wwwweeee ccccaaaannnn uuuunnnnlllliiiinnnnkkkk ffffiiiilllleeee ****////
  1310.            uuuunnnnlllliiiinnnnkkkk((((aaaarrrreeeennnnaaaaffffiiiilllleeee))));;;;
  1311.            }}}}
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.                                   - 21 -
  1325.  
  1326.  
  1327.  
  1328.        Then one uses uspsema(3P) to lock the semaphore, and
  1329.        usvsema(3P) to unlock the semaphore.  Pretty simple:
  1330.  
  1331.  
  1332.        ================================ TTTThhhhrrrreeeeaaaadddd 1111 ========================================================||||============================================ TTTThhhhrrrreeeeaaaadddd 2222 ============================================
  1333.                                        ||||
  1334.              ||||                         ||||          ||||
  1335.              |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee     ||||          |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee
  1336.              ||||                         ||||          ||||
  1337.              ||||                         ||||          ||||
  1338.                                        ||||
  1339.        uuuussssppppsssseeeemmmmaaaa((((FFFFiiiilllleeeeOOOOppppSSSSeeeemmmmaaaa))));;;; ////****lllloooocccckkkk****////   ||||    uuuussssppppsssseeeemmmmaaaa((((FFFFiiiilllleeeeOOOOppppSSSSeeeemmmmaaaa))));;;; ////****lllloooocccckkkk****////
  1340.        aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee1111))));;;;    ||||    aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee2222))));;;;
  1341.        aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;      ||||    aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;
  1342.        uuuussssvvvvsssseeeemmmmaaaa((((FFFFiiiilllleeeeOOOOppppSSSSeeeemmmmaaaa))));;;; ////****uuuunnnnlllloooocccckkkk****//// ||||    uuuussssvvvvsssseeeemmmmaaaa((((FFFFiiiilllleeeeOOOOppppSSSSeeeemmmmaaaa))));;;; ////****uuuunnnnlllloooocccckkkk****////
  1343.                                        ||||
  1344.              ||||                         ||||          ||||
  1345.              |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee     ||||          |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee
  1346.              ||||                         ||||          ||||
  1347.              ||||                         ||||          ||||
  1348.                                        ||||
  1349.  
  1350.        Now, why are we pointing this out for AF users?
  1351.  
  1352.        We have found several applications which were actually
  1353.        committing this error and were GETTING AWAY WITH IT!  By the
  1354.        most arcane of coincidences of CPU scheduler timings, these
  1355.        apps just happened to never fall into the short window of
  1356.        time in which the above worst-case scenario could occur.
  1357.  
  1358.        When these programs were recompiled with an alpha version of
  1359.        a newer Audio File Library, which contains different code
  1360.        and thus has slightly different timing characteristics and
  1361.        gets scheduled slightly differently, the bug in these
  1362.        applications was instantly revealed.
  1363.  
  1364.        This is why we want to alert developers to check their code
  1365.        for this-- there could be a bug of this type in their code
  1366.        which had previously not come out, just because the old AF
  1367.        happens to have had this property (by no design of our own).
  1368.        We want to give developers plenty of advanced warning about
  1369.        this situation.
  1370.  
  1371.        9.7.6  _P_R_O_B_L_E_M__3  Some developers are writing multi-threaded
  1372.        code that would be good under their assumption that the AF
  1373.        is MT/MP-safe. But this assumption is false.
  1374.  
  1375.        The AF is NOT a multi-thread and/or multi-processor safe
  1376.        library, in the following sense:
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.                                   - 22 -
  1391.  
  1392.  
  1393.  
  1394.        Users can make multiple, simultaneous, uncoordinated AF
  1395.        calls on different AFfilehandles from different threads and
  1396.        the library will operate fine.  Each AFfilehandle completely
  1397.        encapsulates the state needed to do operations on that
  1398.        AFfilehandle (except for error handling, which is explained
  1399.        next).
  1400.  
  1401.        Users cannot make multiple, simultaneous, uncoordinated AF
  1402.        calls from different threads to set or access the library's
  1403.        global state--namely, the error handler function.  If two
  1404.        threads simultaneously try and set the error handler (even
  1405.        the same error handler), the behavior is undefined.
  1406.        Furthermore, if the user writes an error handler, then makes
  1407.        multiple, simultaneous, uncoordinated AF calls on different
  1408.        filehandles from different threads, and both AF calls issue
  1409.        an error simultaneously, then two instances of the user's
  1410.        error handler will be called in a simultaneous,
  1411.        uncoordinated manner in two threads.  If this situation is
  1412.        possible in a user's program, the user should use semaphores
  1413.        in their error handler in order to make sure their handler
  1414.        doesn't try and report or deal with two errors at the same
  1415.        time.  Note that any AF function can cause an AF error to
  1416.        occur.  Do not assume a function will not err just because
  1417.        it is simple.
  1418.  
  1419.        A thread-safe error handling routine is now available via
  1420.        the Digital Media Library.  Any application which is
  1421.        expected to function in a multi-threaded fashion should
  1422.        disable the old global _A_F_e_r_r_o_r_h_a_n_d_l_e_r and use
  1423.        _d_m_G_e_t_E_r_r_o_r(3dm) in its place.  Here is a simple example:
  1424.  
  1425.  
  1426.            ####iiiinnnncccclllluuuuddddeeee <<<<ssssttttddddiiiioooo....hhhh>>>>
  1427.            ####iiiinnnncccclllluuuuddddeeee <<<<ddddmmmmeeeeddddiiiiaaaa////ddddmmmmeeeeddddiiiiaaaa....hhhh>>>>
  1428.            ####iiiinnnncccclllluuuuddddeeee <<<<ddddmmmmeeeeddddiiiiaaaa////aaaauuuuddddiiiiooooffffiiiilllleeee....hhhh>>>>
  1429.  
  1430.            ////**** tttthhhhiiiissss rrrroooouuuuttttiiiinnnneeee aaaassssssssuuuummmmeeeessss ffffiiiilllleeee hhhhaaaannnnddddlllleeee iiiissss ooooppppeeeennnn,,,, aaaannnndddd bbbbuuuuffffffffeeeerrrr iiiissss aaaallllllllooooccccaaaatttteeeedddd ****////
  1431.            ////**** rrrreeeettttuuuurrrrnnnnssss nnnnuuuummmmbbbbeeeerrrr ooooffff ffffrrrraaaammmmeeeessss aaaaccccttttuuuuaaaallllllllyyyy rrrreeeeaaaadddd,,,, oooorrrr ----1111 oooonnnn eeeerrrrrrrroooorrrr ****////
  1432.  
  1433.            iiiinnnntttt rrrreeeeaaaaddddBBBBuuuuffffffffeeeerrrr((((AAAAFFFFffffiiiilllleeeehhhhaaaannnnddddlllleeee hhhh,,,, vvvvooooiiiidddd ****bbbbuuuuffffffffeeeerrrr,,,, iiiinnnntttt ffffrrrraaaammmmeeeessss))))
  1434.            {{{{
  1435.                iiiinnnntttt ffffrrrraaaammmmeeeessssRRRReeeeaaaadddd ==== aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,, AAAAFFFF____DDDDEEEEFFFFAAAAUUUULLLLTTTT____TTTTRRRRAAAACCCCKKKK,,,, bbbbuuuuffffffffeeeerrrr,,,, ffffrrrraaaammmmeeeessss))));;;;
  1436.                iiiiffff((((ffffrrrraaaammmmeeeessssRRRReeeeaaaadddd <<<< 0000)))) {{{{ ////**** eeeerrrrrrrroooorrrr!!!! ****////
  1437.                    ffffpppprrrriiiinnnnttttffff((((ssssttttddddeeeerrrrrrrr,,,, """"FFFFaaaattttaaaallll eeeerrrrrrrroooorrrr:::: %%%%ssss\\\\nnnn"""",,,, ddddmmmmGGGGeeeettttEEEErrrrrrrroooorrrr((((NNNNUUUULLLLLLLL,,,, NNNNUUUULLLLLLLL))))))));;;;
  1438.                    rrrreeeettttuuuurrrrnnnn ----1111;;;;
  1439.                }}}}
  1440.                rrrreeeettttuuuurrrrnnnn ffffrrrraaaammmmeeeessssRRRReeeeaaaadddd;;;;
  1441.            }}}}
  1442.  
  1443.        Then elsewhere, prior to this call, you would have:
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.                                   - 23 -
  1457.  
  1458.  
  1459.  
  1460.            aaaaffffSSSSeeeettttEEEErrrrrrrroooorrrrHHHHaaaannnnddddlllleeeerrrr((((NNNNUUUULLLLLLLL))));;;;  ////**** ddddiiiissssaaaabbbblllleeee oooolllldddd----ssssttttyyyylllleeee gggglllloooobbbbaaaallll eeeerrrrrrrroooorrrr hhhhaaaannnnddddlllleeeerrrr ****////
  1461.  
  1462.        Now the most important caveat: users cannot make multiple,
  1463.        simultaneous, uncoordinated AF calls on the SAME
  1464.        AFfilehandle from different threads, even if the order of
  1465.        execution of those calls does not matter to the user.  Doing
  1466.        so will very likely cause a core dump, or at least
  1467.        corruption of the AFfilehandle.  This behavior will never be
  1468.        changed, as we refuse to make our developers pay the price
  1469.        of semaphore locking code at the beginning and end of every
  1470.        afReadFrames and afWriteFrames call.  Most users do not
  1471.        need, and in fact really do not want, semaphore protection
  1472.        that is built-in to the AF calls themselves.
  1473.  
  1474.        Like problem 2, problem 3 can occur with any AF call, not
  1475.        just afSeekFrame() and afReadFrames().  In particular, watch
  1476.        out for simultaneous calls of afSyncFile() and
  1477.        afCloseFile().
  1478.  
  1479.        Note that at no point did we offer any guarantee that the
  1480.        library was MT/MP-safe, and in fact only libraries specified
  1481.        as such in man pages (such as the C library minus errno, as
  1482.        explained in intro(3)) are guaranteed to be so.
  1483.  
  1484.        9.7.7  _P_R_O_B_L_E_M__4  Some developers are using afOpenFD() or
  1485.        afGetFD() to get the file descriptor which an AFfilehandle
  1486.        is using, and then changing the file pointer on that file
  1487.        descriptor.  The AF does not allow this.
  1488.  
  1489.        The file descriptor returned by afGetFD() was intended to be
  1490.        used as part of a select() loop and was not intended to
  1491.        allow users to read, write, and seek in the file without the
  1492.        knowledge of the Audio File Library.  Doing so will cause
  1493.        the library to give unpredictable results unless the user
  1494.        saves and restores the file position whenever they modify
  1495.        it.
  1496.  
  1497.        The reason for this is that we are not willing to force upon
  1498.        users expecting high performance the often very large
  1499.        overhead of an lseek(2) system call on every afReadFrames()
  1500.        or afWriteFrames().  Therefore we cannot restore the file
  1501.        position to a known place on every AF call which relies on
  1502.        the file being in that place.
  1503.  
  1504.        Users who wish to modify the file position of the file
  1505.        descriptor given by afGetFD() should save the file position
  1506.        and restore it after they are finished and before they make
  1507.        the next AF call to read or write data to the file.  The new
  1508.        routines _a_f_S_a_v_e_F_i_l_e_P_o_s_i_t_i_o_n(3dm) and
  1509.        _a_f_R_e_s_t_o_r_e_F_i_l_e_P_o_s_i_t_i_o_n(3dm) have been created expressly for
  1510.        this purpose.  Alternately they can use one of two different
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.                                   - 24 -
  1523.  
  1524.  
  1525.  
  1526.        file descriptors opened to the same file (dup(2) will not
  1527.        work; it gives you two file descriptors which share a file
  1528.        offset.  The file must be re-opened in order to get a
  1529.        separate file descriptor).  Also, if users attempt to write
  1530.        to the file, no matter how the AFfilehandle was opened, the
  1531.        results are undefined.
  1532.  
  1533.        Developers had asked for a call to get the offset of the
  1534.        audio data in an audio file.  Two new functions have been
  1535.        made available.  afGetDataOffset(3dm) returns the offset in
  1536.        bytes from the beginning of an open audio file to the first
  1537.        audio sample.  afGetTrackBytes(3dm) returns the size in
  1538.        bytes of the audio data portion of a given track in an audio
  1539.        file.
  1540.  
  1541.        9.7.8  _W_A_R_N_I_N_G  We give no guarantees about the number or
  1542.        nature of UNIX system calls that will result from a given AF
  1543.        call.  In particular, afReadFrames() and afWriteFrames()
  1544.        could actually read() or write() any amount of data in the
  1545.        file, or could read() or write() more than once in varying
  1546.        chunk sizes.  Also, afOpenFile(), afSeekFrame(),
  1547.        afSyncFile(), afCloseFile(), and other AF functions could
  1548.        result in any amount of data being read from or written to
  1549.        the file.  (but of course the AF will not write to a file
  1550.        opened for read access or read from a file opened for write
  1551.        access).
  1552.  
  1553.        Users who are attempting to optimize the I/O in their
  1554.        program by managing I/O system call behavior should be aware
  1555.        that at this time we offer no guarantees about when the AF
  1556.        will perform system calls.
  1557.  
  1558.        This has not been a problem to our knowledge, but we thought
  1559.        it would be a good idea just to make this explicit.  At this
  1560.        time we offer no guarantees that a read of, say, 50,000
  1561.        frames will result in a single UNIX read() system call for
  1562.        exactly 50,000 frames worth of data.  Although we will
  1563.        strive to make this the case whenever it seems beneficial,
  1564.        there are certain cases where this is simply impossible.
  1565.        For example, reads from or writes to a compressed file will
  1566.        generally be split up into a chunksize which is natural for
  1567.        the codec being used, and thus possibly result in several
  1568.        reads or writes to the file.  Also, we give no guarantee
  1569.        that afSeekFrame() will seek the file at all in the UNIX
  1570.        lseek() sense, although it happens that this is generally
  1571.        the case for the current Audio File Library.  And even if
  1572.        afSeekFrame() does lseek() the file, we do not guarantee
  1573.        that it will be to the exact frame the user specified
  1574.        (though this is generally the case in the old AF library).
  1575.        This is due to necessary compensation for filter delays
  1576.        inherent in certain forms of compression and decompression.
  1577.  
  1578.  
  1579.  
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.                                   - 25 -
  1589.  
  1590.  
  1591.  
  1592.        In a future release, features will be introduced which may
  1593.        make this more of an issue.  At that time we will come up
  1594.        with much more specific information about what guarantees we
  1595.        can offer to the developer.  We want to find out if these
  1596.        kinds of guarantees are something that developers need at
  1597.        all.  Perhaps it is simply not an issue for developers.  But
  1598.        if it is, we would be most interested in hearing your
  1599.        feedback.  You can contact your local SGI representative and
  1600.        ask them to send a message to the developers of the Audio
  1601.        File Library, or you can also use the newsgroup
  1602.        comp.sys.sgi.audio for faster and much more direct contact.
  1603.  
  1604.  
  1605.  
  1606.  
  1607.  
  1608.  
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.  
  1619.  
  1620.  
  1621.  
  1622.  
  1623.  
  1624.  
  1625.  
  1626.  
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.  
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.